<?php

namespace builder\test\controllers;

use builder\base\BaseController;
use builder\tree\TreeBuilder;

// Tree Builder Example
class TreeController extends BaseController
{
    public $publicActions = ['index', 'demo'];

    public function actionDemo()
    {
        if ($this->isAjax) {
            return $this->asOk('Success');
        } else {
            return 'Success';
        }
    }

    //Demo
    public function actionIndex()
    {
        $treeBuilder = TreeBuilder::instance(['id' => 'Tree_Index']);
        if ($this->isAjax) {
            $treeBuilder->setData([
                [
                    'title' => '醉里', 'id' => 1, 'children' => [
                        ['title' => '挑灯', 'id' => 11, 'children' => [
                                ['title' => '看剑', 'id' => 12,],
                                ['title' => '梦回吹角连营', 'id' => 121,]
                            ],
                        ]
                    ],
                ],
                [
                    'title' => '窗前', 'id' => 2, 'children' => [
                        ['title' => '明月', 'id' => 21, 'children' => [
                                ['title' => '光', 'id' => 22,],
                                ['title' => '疑是地上霜', 'id' => 221,]
                            ],
                        ]
                    ],
                ],
            ]);
        } else {
            $extraCss = <<<'CSS'
                .btn-bottom-group{text-align:right;}
CSS;
            $treeBuilder->setTitle('权限')
                ->setPartial()
                ->setTreeLayout([
                    'class' => ['h-500', 'overflow-auto'],
                ])
                ->setExtraCss($extraCss)
                ->setCustomName([
                    'title' => 'name',
                ])
                ->setSwitchCheckedBtn()
                ->setSwitchSpreadBtn()
                ->setOperationItem([
                    $this->operationItem->ajax()
                        ->positionBottom()
                        ->route('ymtest/tree/demo')
                        ->title('立即提交')
                        ->params(['id'])
                        ->closeModal()
                        ->questionDescription('确认当前权限分配吗?')
                        ->method('POST'),
                    $this->operationItem->modal('测试一下')
                        ->title('123')
                        ->route('ymtest/tree/demo')
                        ->titleMaxminBtnDisplay()
                        ->params(['id']),
                ]);
        }

        return $treeBuilder->render();
    }
}